<template>
  <div class="app-container user">
    <SearchForm
      v-model:form-data="form"
      @search="handleSearch"
    />
    <PaginationCard
      v-model:currentPage="paginationData.currentPage"
      v-model:pageSize="paginationData.pageSize"
      :total="paginationData.total"
      :load-list="getTableData"
    >
      <AppTable
        :data="tableData"
        :columns="columns"
        border
      />
    </PaginationCard>
    <EntityChangeDetail
      v-model="visible"
      v-model:entityChangeId="entityChangeId"
      size="40%"
    />
  </div>
</template>

<script lang="tsx" setup>
import { onMounted, reactive, ref } from 'vue'
import SearchForm from './SearchForm.vue'
import AppTable from '@/components/AppTable/index.vue'
import AppButtonGroup from '@/components/AppButtonGroup/index.vue'
import PaginationCard from '@/components/PaginationCard/index.vue'
import EntityChangeDetail from '@/views/system/auditLog/EntityChangeDetail.vue'
import { getEntityChangeLog } from '@/http/entityChangeLog'
import { EntityLogEnum, IEntityChangeLog } from '@/http/types/entityChangeLog'
import { parseDate2Str } from '@/utils/timeUtils'
import { IButtonConfig } from '@/components/AppButtonGroup/type'

const buttonConfig:IButtonConfig[] = [
  {
    name: '详细信息',
    eventName: 'handleDetail',
    type: 'primary'
  }
]
const columns = ref([
  { label: 'AuditLogId', prop: 'auditLogId' },
  { label: 'EntityTypeFullName', prop: 'entityTypeFullName' },
  { label: 'EntityId', prop: 'entityId' },
  { label: 'ChangeType', prop: 'changeType', render: ({ row }: {row: IEntityChangeLog}) => EntityLogEnum[row.changeType] },
  { label: 'ChangeTime', prop: 'ChangeTime', render: ({ row }: {row: IEntityChangeLog}) => parseDate2Str(row.changeTime, 'YYYY-MM-DD HH:mm:ss') },
  {
    label: '操作',
    fixed: 'right',
    width: 220,
    render: ({ row }:{row:IEntityChangeLog}) => {
      return <AppButtonGroup
                rowData={row}
                buttonConfigs={ buttonConfig }
                onEvent={(eventName:string) => {
                  eventName === 'handleDetail' && handleDetail(row)
                }}
              >
             </AppButtonGroup>
    }
  }
])

interface IFormData{
  auditLogId: string
  date: [string | Date, string | Date]
  changeType: 0 | 1 | 2 | ''
  entityId: string
  entityTypeFullName: string
}
const form = ref<IFormData>({
  auditLogId: '',
  date: ['', ''],
  changeType: '',
  entityId: '',
  entityTypeFullName: ''
})

const paginationData = reactive({
  pageSize: 10,
  currentPage: 1,
  total: 0
})
const tableData = ref<IEntityChangeLog[]>([])

onMounted(() => {
  getTableData()
})
const getTableData = async () => {
  const res = await getEntityChangeLog(
    paginationData.currentPage,
    paginationData.pageSize,
    {
      auditLogId: form.value.auditLogId,
      startTime: form.value.date[0],
      endTime: form.value.date[1],
      changeType: form.value.changeType,
      entityId: form.value.entityId,
      entityTypeFullName: form.value.entityTypeFullName
    })
  tableData.value = res.items
  paginationData.total = res.totalCount
}

const handleSearch = () => {
  paginationData.currentPage = 1
  getTableData()
}

const visible = ref(false)
const entityChangeId = ref('')
const handleDetail = (detail: IEntityChangeLog) => {
  entityChangeId.value = detail.id
  visible.value = true
}
</script>

<style lang="scss" scoped>
</style>
